package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.dto.MessageRabbitMqDTO;
import com.xbongbong.pro.message.pojo.vo.MessageRabbitMqVO;
import com.xbongbong.pro.message.service.MessageService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 审批 及时消息推送相关接口
 * @author pkh
 * @date 2022/7/6 15:23
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/message/mq/approval")
public class MessageRabbitMqApprovalController {
    @Resource
    private MessageService messageService;
    /**
     * Description: 新增审批评价
     * @param messageRabbitMqDTO 消息推送  走消息队列，用于传输数据的
     * @param br
     * @return java.lang.String
     * @throws Exception Exception
     * @author pkh
     * @date 2022/7/6 15:23
     * @since v1.0
     */
    @RequestMapping(value = "/comment", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String comment(@RequestBody @Valid MessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception{
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                messageService.delayPushDingtalkOAMessage(messageRabbitMqDTO, PushTypeEnum.APPROVAL_PUSH_ADD_COMMENT);
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
